package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcKqworks;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.service.IGcKqworksService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 每日出勤情况登记Controller
 * 
 * @author ruoyi
 * @date 2023-04-28
 */
@Controller
@RequestMapping("/gcgl/kqworks")
public class GcKqworksController extends BaseController
{
    private String prefix = "gcgl/kqworks";

    @Autowired
    private IGcKqworksService gcKqworksService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    @RequiresPermissions("gcgl:kqworks:view")
    @GetMapping()
    public String kqworks()
    {
        return prefix + "/kqworks";
    }

    /**
     * 查询每日出勤情况登记列表
     */
    @RequiresPermissions("gcgl:kqworks:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcKqworks gcKqworks)
    {
        startPage();
        gcKqworks.setDeleteFlag(1);
        gcKqworks.setCreateUser(getLoginName());
        List<GcKqworks> list = gcKqworksService.selectGcKqworksList(gcKqworks);
        for(GcKqworks obj:list){
        	if(obj.getZlwj()!=null && !obj.getZlwj().equals("")){
        	obj.setZlwj(RuoYiConfig.getOnPath()+obj.getZlwj());
        	}
        }
        return getDataTable(list);
    }

    /**
     * 导出每日出勤情况登记列表
     */
    @RequiresPermissions("gcgl:kqworks:export")
    @Log(title = "每日出勤情况登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcKqworks gcKqworks)
    {
        List<GcKqworks> list = gcKqworksService.selectGcKqworksList(gcKqworks);
        ExcelUtil<GcKqworks> util = new ExcelUtil<GcKqworks>(GcKqworks.class);
        return util.exportExcel(list, "每日出勤情况登记数据");
    }

    /**
     * 新增每日出勤情况登记
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	mmap.put("rq", DateUtils.getDate());
    	
    	Long[] deptIds = {112l,113l,114l,115l,116l,117l,118l,119l};
    	List<SysUser> sysUserlis = userService.selectUserByDeptIds(deptIds);
    	mmap.put("sysUserlis", sysUserlis);
    	
    	GcProject gcProject = new GcProject();
    	gcProject.setDeleteFlag(1);
    	List<GcProject> projectlis = gcProjectService.selectGcProjectList(gcProject);
    	mmap.put("projectlis", projectlis);
    	for(GcProject obj:projectlis){
    		obj.setName("("+obj.getOrderNo()+")"+obj.getName());
    	}
    	return prefix + "/add";
    }

    /**
     * 新增保存每日出勤情况登记
     */
    @RequiresPermissions("gcgl:kqworks:add")
    @Log(title = "每日出勤情况登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcKqworks gcKqworks,@RequestParam("zilwjfile")MultipartFile filewj)
    {
    	
    	try {
    		if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcKqworks.setZlwj(zilwj);
            }
    		String userName="";
    		List<SysUser> userlis = userService.selectUserInIds(gcKqworks.getPeopleIds().split(","));
    		for(SysUser user:userlis){
    			userName+=user.getUserName()+",";
    		}
    		gcKqworks.setCount(userlis.size());
    		gcKqworks.setJig(
    				Double.valueOf(Double.parseDouble(gcKqworks.getDescs().toString())*userlis.size()));
    		gcKqworks.setPeopleName(userName.substring(0,userName.length()-1));
    		gcKqworks.setStatus(0);
    		gcKqworks.setDeleteFlag(1);
    		gcKqworks.setCreateAt(new Date());
    		gcKqworks.setUpdateAt(new Date());
    		gcKqworks.setCreateUser(getLoginName());
    		gcKqworks.setUpdateUser(getLoginName());
            gcKqworksService.insertGcKqworks(gcKqworks);
		} catch (Exception e) {
			// TODO: handle exception
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 修改每日出勤情况登记
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcKqworks gcKqworks = gcKqworksService.selectGcKqworksById(id);
        if(gcKqworks.getZlwj()!=null && !gcKqworks.getZlwj().equals("")){
        gcKqworks.setZlwj(RuoYiConfig.getOnPath()+gcKqworks.getZlwj());
        }
        mmap.put("gcKqworks", gcKqworks);
        
        Long[] deptIds = {112l,113l,114l,115l,116l,117l,118l,119l};
    	List<SysUser> sysUserlis = userService.selectUserByDeptIds(deptIds);
    	mmap.put("sysUserlis", sysUserlis);
    	
    	String peopleIds = ","+gcKqworks.getPeopleIds()+",";
    	for(SysUser obj:sysUserlis){
//    		flag
    		String userid = ","+obj.getUserId().toString()+",";
    		if(peopleIds.indexOf(userid)!=-1){
    			obj.setFlag(true);
    		}else{
    			obj.setFlag(false);
    		}
    	}
    	mmap.put("sysUserlis", sysUserlis);
    	
    	GcProject gcProject = new GcProject();
    	gcProject.setDeleteFlag(1);
    	List<GcProject> projectlis = gcProjectService.selectGcProjectList(gcProject);
    	for(GcProject obj:projectlis){
    		obj.setName("("+obj.getOrderNo()+")"+obj.getName());
    	}
    	mmap.put("projectlis", projectlis);
        return prefix + "/edit";
    }

    /**
     * 修改保存每日出勤情况登记
     */
    @Log(title = "每日出勤情况登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcKqworks gcKqworks,@RequestParam("zilwjfile")MultipartFile filewj)
    {
    	
    	try {
    		if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcKqworks.setZlwj(zilwj);
            }
    		
    		String userName="";
    		List<SysUser> userlis = userService.selectUserInIds(gcKqworks.getPeopleIds().split(","));
    		for(SysUser user:userlis){
    			userName+=user.getUserName()+",";
    		}
    		gcKqworks.setJig(
    				Double.valueOf(Double.parseDouble(gcKqworks.getDescs().toString())*userlis.size()));
    		gcKqworks.setPeopleName(userName.substring(0,userName.length()-1));
    		gcKqworks.setCount(userlis.size());
    		gcKqworksService.updateGcKqworks(gcKqworks);
		} catch (Exception e) {
			// TODO: handle exception
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 删除每日出勤情况登记
     */
    @RequiresPermissions("gcgl:kqworks:remove")
    @Log(title = "每日出勤情况登记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcKqworksService.deleteGcKqworksByIds(ids));
    }
    
    @GetMapping("/cgqk")
    @ResponseBody
    public AjaxResult cgqk(){
    	SysDictData dictData = new SysDictData();
    	dictData.setDictType("gcgl_cgqk");
    	List<SysDictData> dictDatelis = sysDictDataService.selectDictDataList(dictData);
    	List<Map> replis = new ArrayList<Map>();
    	for(SysDictData dict:dictDatelis){
    		Map rep = new HashMap();
    		rep.put("dictValue", dict.getDictLabel());
    		replis.add(rep);
    	}
    	
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", replis);
        return ajax;
    }
}